package com.carrentalssm.controller;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.sql.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.carrentalssm.entity.CarRental;
import com.carrentalssm.entity.User;
import com.carrentalssm.service.CarRentalService;

/**
 *
 */
@Controller
@RequestMapping("/carRental")
public class CarRentalController {
	
	@Autowired
	private CarRentalService carRentalService;
	
	/**
	 * 添加租赁订单
	 * @param carRental
	 * @return
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public String add(
			@RequestParam("carId") String carId,
			@RequestParam("rentalNum") String rentalNum,
			@RequestParam("rentalTotal") String rentalTotal,
			@RequestParam("rentalStartDatetime") String rentalStartDatetime,
			@RequestParam("rentalEndDatetime") String rentalEndDatetime,
			HttpServletRequest request) {
		
		User user = (User) request.getSession().getAttribute("user");
		
		String username = user.getUsername();
		
		CarRental carRental = new CarRental();
		carRental.setId(new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new java.util.Date()));
		carRental.setCarId(carId);
		carRental.setUsername(username);
		carRental.setRentalNum(Integer.parseInt(rentalNum));
		carRental.setRentalTotal(Double.valueOf(rentalTotal));
		carRental.setRentalStartDatetime(Date.valueOf(rentalStartDatetime));
		carRental.setRentalEndDatetime(Date.valueOf(rentalEndDatetime));
		carRental.setCreateDatetime(new Timestamp(new java.util.Date().getTime()));
		
		carRentalService.add(carRental);
		return "redirect:/carRentalList.jsp";
	}

}
